﻿using Ionic.Zip;
using System;
using System.IO;

public class FileProcessor
{
    public string TempFolderPath { get; private set; }

    public FileProcessor(string temporaryFolderPath)
    {
        this.TempFolderPath = temporaryFolderPath;
    }

    // Extract zip archive in temporary directory
    public void UnzipFile(string zipFilePath)
    {
        using (ZipFile zip = ZipFile.Read(zipFilePath))
        {
            foreach (ZipEntry e in zip)
            {
                e.Extract(TempFolderPath, ExtractExistingFileAction.OverwriteSilently);
            }
        }
    }

    // Delete temporary directory and its content
    public void DeleteTempDirectory()
    {
        DirectoryInfo tempFolder = new DirectoryInfo(TempFolderPath);
        tempFolder.Delete(true);
    }

    // Move zip file into  archive directory and rename it
    public void MoveZipFile(string salesReportsZipFile)
    {
        // Create destination path
        string path = salesReportsZipFile;
        string fileName = salesReportsZipFile.Substring(salesReportsZipFile.LastIndexOf('\\') + 1);
        DateTime currentDateTime = DateTime.Now;
        fileName = String.Format("{0}-{1}-{2}_{3}-{4}-{5}-", 
            currentDateTime.Year, currentDateTime.Month, currentDateTime.Day, 
            currentDateTime.Hour, currentDateTime.Minute, currentDateTime.Second) + fileName;
        string oldPath = salesReportsZipFile.Substring(0, salesReportsZipFile.LastIndexOf('\\'));
        string newPath = oldPath.Substring(0, oldPath.LastIndexOf('\\') + 1) + "SalesReportArchive\\" + fileName;
        
        // Ensure that the target does not exist. 
        if (File.Exists(newPath))
        {
            File.Delete(newPath);
        }

        // Move the file.
        File.Move(path, newPath);
    }
}
